﻿using Microsoft.Extensions.Configuration;
using StarsAdmin.Core.Helpers;

namespace StarsAdmin.Core.App.Models
{
    public class Cors
    {
        private readonly IConfiguration _configuration;
        private string _allowCors;
        private string[] _specificOrigins;

        public Cors(IConfiguration configuration)
        {
            _configuration = configuration.NotNull();
            Initialize();
        }

        /// <summary>
        /// 允许所有来源
        /// </summary>
        public string AllowCors { get => _allowCors; }

        /// <summary>
        /// 允许的特定来源数组
        /// </summary>
        public string[] SpecificOrigins { get => _specificOrigins; }

        private void Initialize()
        {
            var corsConfiguration = _configuration.GetSection("CorsConfiguration");
            _allowCors = corsConfiguration.GetSection("AllowCors").Value.NotNull();
            _specificOrigins = corsConfiguration.GetSection("SpecificOrigins").Get<string[]>().NotNull();
        }
    }
}